/*********************************************************************
 *
 *  Main Application Entry Point and TCP/IP Stack Demo
 *  Module for Microchip TCP/IP Stack
 *   -Demonstrates how to call and use the Microchip TCP/IP stack
 *	 -Reference: AN833
 *
 *********************************************************************
 * FileName:        MainDemo.c
 * Dependencies:    TCPIP.h
 * Processor:       PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
 * Compiler:        Microchip C32 v1.05 or higher
 *					Microchip C30 v3.12 or higher
 *					Microchip C18 v3.30 or higher
 *					HI-TECH PICC-18 PRO 9.63PL2 or higher
 * Company:         Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * Copyright (C) 2002-2010 Microchip Technology Inc.  All rights
 * reserved.
 *
 * Microchip licenses to you the right to use, modify, copy, and
 * distribute:
 * (i)  the Software when embedded on a Microchip microcontroller or
 *      digital signal controller product ("Device") which is
 *      integrated into Licensee's product; or
 * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
 *		ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
 *		used in conjunction with a Microchip ethernet controller for
 *		the sole purpose of interfacing with the ethernet controller.
 *
 * You should refer to the license agreement accompanying this
 * Software for additional information regarding your rights and
 * obligations.
 *
 * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
 * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
 * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
 * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
 * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
 * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
 * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
 *
 *
 * Author              Date         Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Nilesh Rajbharti		4/19/01		Original (Rev. 1.0)
 * Nilesh Rajbharti		2/09/02		Cleanup
 * Nilesh Rajbharti		5/22/02		Rev 2.0 (See version.log for detail)
 * Nilesh Rajbharti		7/9/02		Rev 2.1 (See version.log for detail)
 * Nilesh Rajbharti		4/7/03		Rev 2.11.01 (See version log for detail)
 * Howard Schlunder		10/1/04		Beta Rev 0.9 (See version log for detail)
 * Howard Schlunder		10/8/04		Beta Rev 0.9.1 Announce support added
 * Howard Schlunder		11/29/04	Beta Rev 0.9.2 (See version log for detail)
 * Howard Schlunder		2/10/05		Rev 2.5.0
 * Howard Schlunder		1/5/06		Rev 3.00
 * Howard Schlunder		1/18/06		Rev 3.01 ENC28J60 fixes to TCP, 
 *									UDP and ENC28J60 files
 * Howard Schlunder		3/01/06		Rev. 3.16 including 16-bit micro support
 * Howard Schlunder		4/12/06		Rev. 3.50 added LCD for Explorer 16
 * Howard Schlunder		6/19/06		Rev. 3.60 finished dsPIC30F support, added PICDEM.net 2 support
 * Howard Schlunder		8/02/06		Rev. 3.75 added beta DNS, NBNS, and HTTP client (GenericTCPClient.c) services
 * Howard Schlunder		12/28/06	Rev. 4.00RC added SMTP, Telnet, substantially modified TCP layer
 * Howard Schlunder		04/09/07	Rev. 4.02 added TCPPerformanceTest, UDPPerformanceTest, Reboot and fixed some bugs
 * Howard Schlunder		xx/xx/07	Rev. 4.03
 * HSchlunder & EWood	08/27/07	Rev. 4.11
 * HSchlunder & EWood	10/08/07	Rev. 4.13
 * HSchlunder & EWood	11/06/07	Rev. 4.16
 * HSchlunder & EWood	11/08/07	Rev. 4.17
 * HSchlunder & EWood	11/12/07	Rev. 4.18
 * HSchlunder & EWood	02/11/08	Rev. 4.19
 * HSchlunder & EWood   04/26/08    Rev. 4.50 Moved most code to other files for clarity
 * KHesky               07/07/08    Added ZG2100-specific support
 * HSchlunder & EWood   07/24/08    Rev. 4.51
 * Howard Schlunder		11/10/08    Rev. 4.55
 * Howard Schlunder		04/14/09    Rev. 5.00
 * Howard Schlunder		07/10/09    Rev. 5.10
 * Howard Schlunder		11/18/09    Rev. 5.20
 * Howard Schlunder		04/28/10    Rev. 5.25
 ********************************************************************/
/*
 * This macro uniquely defines this file as the main entry point.
 * There should only be one such definition in the entire project,
 * and this file must define the AppConfig variable as described below.
 */
#define THIS_IS_STACK_APPLICATION

// Include all headers for any enabled TCPIP Stack functions
#include "TCPIP Stack/TCPIP.h"

#if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
#include "TCPIP Stack/ZeroconfLinkLocal.h"
#endif
#if defined(STACK_USE_ZEROCONF_MDNS_SD)
#include "TCPIP Stack/ZeroconfMulticastDNS.h"
#endif

// Include functions specific to this stack application
#include "MainDemo.h"

// alb: INIZIO CONF BITS
#include "p18f67j60.h"
#pragma config DEBUG = OFF, STVR = OFF, CP0 = OFF, IESO = OFF, FCMEN = OFF, WDTPS = 32768
// alb: FINE CONF BITS

#pragma udata my_section_1
unsigned int Port80_oppure_8085;
unsigned int Port80_oppure_xxx;
BYTE NameRel1[15];
BYTE NameRel2[15];
BYTE NameRel3[15];
BYTE NameRel4[15];
BYTE NameRel5[15];
BYTE NameRel6[15];
BYTE NameRel7[15];
BYTE NameRel8[15];
#pragma udata my_section_2
unsigned int MappaIn1 = 1;
unsigned int MappaIn2 = 2;
unsigned int MappaIn3 = 3;
unsigned int MappaIn4 = 4;
unsigned int MappaIn5 = 5;
unsigned int MappaIn6 = 6;
unsigned int MappaIn7 = 7;
unsigned int MappaIn8 = 8;

unsigned int CntDurataRele1 = 65534;
unsigned int CntDurataRele2 = 65534;
unsigned int CntDurataRele3 = 65534;
unsigned int CntDurataRele4 = 65534;
unsigned int CntDurataRele5 = 65534;
unsigned int CntDurataRele6 = 65534;
unsigned int CntDurataRele7 = 65534;
unsigned int CntDurataRele8 = 65534;

unsigned int FixDurataRele1 = 65533;
unsigned int FixDurataRele2 = 65533;
unsigned int FixDurataRele3 = 65533;
unsigned int FixDurataRele4 = 65533;
unsigned int FixDurataRele5 = 65533;
unsigned int FixDurataRele6 = 65533;
unsigned int FixDurataRele7 = 65533;
unsigned int FixDurataRele8 = 65533;

#pragma udata my_section_3
BYTE CntFiltroIn1 = 0;
BYTE CntFiltroIn2 = 0;
BYTE CntFiltroIn3 = 0;
BYTE CntFiltroIn4 = 0;
BYTE CntFiltroIn5 = 0;
BYTE CntFiltroIn6 = 0;
BYTE CntFiltroIn7 = 0;
BYTE CntFiltroIn8 = 0;

BOOL ComandoRele1Eseguito = FALSE;
BOOL ComandoRele2Eseguito = FALSE;
BOOL ComandoRele3Eseguito = FALSE;
BOOL ComandoRele4Eseguito = FALSE;
BOOL ComandoRele5Eseguito = FALSE;
BOOL ComandoRele6Eseguito = FALSE;
BOOL ComandoRele7Eseguito = FALSE;
BOOL ComandoRele8Eseguito = FALSE;

BOOL ComandoRele_1Eseguito = FALSE;
BOOL ComandoRele_2Eseguito = FALSE;
BOOL ComandoRele_3Eseguito = FALSE;
BOOL ComandoRele_4Eseguito = FALSE;
BOOL ComandoRele_5Eseguito = FALSE;
BOOL ComandoRele_6Eseguito = FALSE;
BOOL ComandoRele_7Eseguito = FALSE;
BOOL ComandoRele_8Eseguito = FALSE;

BYTE DurataRele1[5] = {'0', '0', '0', '0', 0};
BYTE DurataRele2[5] = {'0', '0', '0', '0', 0};
BYTE DurataRele3[5] = {'0', '0', '0', '0', 0};
BYTE DurataRele4[5] = {'0', '0', '0', '0', 0};
BYTE DurataRele5[5] = {'0', '0', '0', '0', 0};
BYTE DurataRele6[5] = {'0', '0', '0', '0', 0};
BYTE DurataRele7[5] = {'0', '0', '0', '0', 0};
BYTE DurataRele8[5] = {'0', '0', '0', '0', 0};

BYTE contatore_toggle1 = 0;
BYTE contatore_toggle2 = 0;
BYTE contatore_toggle3 = 0;
BYTE contatore_toggle4 = 0;
BYTE contatore_toggle5 = 0;
BYTE contatore_toggle6 = 0;
BYTE contatore_toggle7 = 0;
BYTE contatore_toggle8 = 0;
BYTE contatore_toggle9 = 0;
BYTE contatore_toggle10 = 0;
BYTE contatore_toggle11 = 0;
BYTE contatore_toggle12 = 0;

#pragma udata my_section_4
// Declare AppConfig structure and some other supporting stack variables
APP_CONFIG AppConfig;
DWORD sizeOfNameRels = 15;
/* alb:
 * memory starts at 0
 * 127 is size of APPCONFIG
 * 1 is address 0 set to 0x61
 */
#define     extEEaddr_NameRel1  sizeof (AppConfig) +1 // alb: 127+1=128
#define     extEEaddr_NameRel2  extEEaddr_NameRel1 +sizeOfNameRels // alb: 128+15=143
#define     extEEaddr_NameRel3  extEEaddr_NameRel2 +sizeOfNameRels // alb: 143+15=158
#define     extEEaddr_NameRel4  extEEaddr_NameRel3 +sizeOfNameRels // alb: 158+15=173
#define     extEEaddr_NameRel5  extEEaddr_NameRel4 +sizeOfNameRels // alb: 173+15=188
#define     extEEaddr_NameRel6  extEEaddr_NameRel5 +sizeOfNameRels // alb: 188+15=203
#define     extEEaddr_NameRel7  extEEaddr_NameRel6 +sizeOfNameRels // alb: 203+15=218
#define     extEEaddr_NameRel8  extEEaddr_NameRel7 +sizeOfNameRels // alb: 218+15=233
#define     extEEaddr_MappaIn   extEEaddr_NameRel8 +sizeOfNameRels // alb: 233+15=248
#define     extEEaddr_DurataRele_part1  extEEaddr_MappaIn +8 // alb: 248 +8 = 256
#define     extEEaddr_DurataRele_part2 extEEaddr_DurataRele_part1 +16 // alb: 256 +16 = 272
#define     extEEaddr_NumPorta extEEaddr_DurataRele_part2 +16 // alb: 272 +16 = 288
// BYTE AN0String[8];  andreasnt

// Use UART2 instead of UART1 for stdout (printf functions).  Explorer 16 
// serial port hardware is on PIC UART2 module.
#if defined(EXPLORER_16) || defined(PIC24FJ256DA210_DEV_BOARD)
int __C30_UART = 2;
#endif


// Private helper functions.
// These may or may not be present in all applications.
static void InitAppConfig(void);
static void InitializeBoard(void);
BYTE clear(BYTE * c);

// static void ProcessIO(void);

//
// PIC18 Interrupt Service Routines
// 
// NOTE: Several PICs, including the PIC18F4620 revision A3 have a RETFIE FAST/MOVFF bug
// The interruptlow keyword is used to work around the bug when using C18
#if defined(__18CXX)
#if defined(HI_TECH_C)
void interrupt low_priority LowISR(void)
#else
#pragma interruptlow LowISR

void LowISR(void)
#endif
{
    TickUpdate();
}

#if defined(HI_TECH_C)
void interrupt HighISR(void)
#else
#pragma interruptlow HighISR

void HighISR(void)
#endif
{
#if defined(STACK_USE_UART2TCP_BRIDGE)
    UART2TCPBridgeISR();
#endif

#if defined(WF_CS_TRIS)
    WFEintISR();
#endif // WF_CS_TRIS
}

#if !defined(HI_TECH_C)
#pragma code lowVector=0x18

void LowVector(void) {
    _asm goto LowISR _endasm
}
#pragma code highVector=0x8

void HighVector(void) {
    _asm goto HighISR _endasm
}
#pragma code // Return to default code section
#endif

// C30 and C32 Exception Handlers
// If your code gets here, you either tried to read or write
// a NULL pointer, or your application overflowed the stack
// by having too many local variables or parameters declared.
#elif defined(__C30__)

void _ISR __attribute__((__no_auto_psv__)) _AddressError(void) {
    Nop();
    Nop();
}

void _ISR __attribute__((__no_auto_psv__)) _StackError(void) {
    Nop();
    Nop();
}

#elif defined(__C32__)

void _general_exception_handler(unsigned cause, unsigned status) {
    Nop();
    Nop();
}
#endif

//
// Main application entry point.
//

void main(void) {
    static TICK t_lampeggio = 0;
    BYTE c[16];
    BYTE *p_c;
    unsigned char j = 0;
    static DWORD dwLastIP = 0;

    // Initialize application specific hardware
    InitializeBoard();

    /* alb: to be used in case of panic
    XEEBeginWrite(0x0000);
    XEEWrite(0x62);
    XEEEndWrite();
    */

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_NameRel1, c, 0x10); // alb: ExtEPROM begin at 128
    NameRel1[0] = c[0];
    if (NameRel1[0] == 0xFF) {
        NameRel1[0] = 'R';
        NameRel1[1] = 'E';
        NameRel1[2] = 'L';
        NameRel1[3] = 'E';
        NameRel1[4] = '1';
        NameRel1[5] = 0;
    } else {
        for (j = 1; j < 14; j++) {
            NameRel1[j] = c[j];
            Nop();
            Nop();
            Nop();
            Nop();
        }
    }
    NameRel1[14] = 0;

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_NameRel2, c, 0x10);
    NameRel2[0] = c[0];
    if (NameRel2[0] == 0xFF) {
        NameRel2[0] = 'R';
        NameRel2[1] = 'E';
        NameRel2[2] = 'L';
        NameRel2[3] = 'E';
        NameRel2[4] = '2';
        NameRel2[5] = 0;

    } else {
        for (j = 1; j < 14; j++) {
            NameRel2[j] = c[j];
            Nop();
            Nop();
            Nop();
            Nop();
        }
    }
    NameRel2[14] = 0;

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_NameRel3, c, 0x10);
    NameRel3[0] = c[0];
    if (NameRel3[0] == 0xFF) {
        NameRel3[0] = 'R';
        NameRel3[1] = 'E';
        NameRel3[2] = 'L';
        NameRel3[3] = 'E';
        NameRel3[4] = '3';
        NameRel3[5] = 0;
    } else {
        for (j = 1; j < 14; j++) {
            NameRel3[j] = c[j];
            Nop();
            Nop();
            Nop();
            Nop();
        }
    }
    NameRel3[14] = 0;

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_NameRel4, c, 0x10);
    NameRel4[0] = c[0];
    if (NameRel4[0] == 0xFF) {
        NameRel4[0] = 'R';
        NameRel4[1] = 'E';
        NameRel4[2] = 'L';
        NameRel4[3] = 'E';
        NameRel4[4] = '4';
        NameRel4[5] = 0;
    } else {
        for (j = 1; j < 14; j++) {
            NameRel4[j] = c[j];
            Nop();
            Nop();
            Nop();
            Nop();
        }
    }
    NameRel4[14] = 0;

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_NameRel5, c, 0x10);
    NameRel5[0] = c[0];
    if (NameRel5[0] == 0xFF) {
        NameRel5[0] = 'R';
        NameRel5[1] = 'E';
        NameRel5[2] = 'L';
        NameRel5[3] = 'E';
        NameRel5[4] = '5';
        NameRel5[5] = 0;
    } else {
        for (j = 61; j < (60 + 14); j++) {
            NameRel5[j] = c[j];
            Nop();
            Nop();
            Nop();
            Nop();
        }
    }
    NameRel5[14] = 0;

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_NameRel6, c, 0x10);
    NameRel6[0] = c[0];
    if (NameRel6[0] == 0xFF) {
        NameRel6[0] = 'R';
        NameRel6[1] = 'E';
        NameRel6[2] = 'L';
        NameRel6[3] = 'E';
        NameRel6[4] = '6';
        NameRel6[5] = 0;
    } else {
        for (j = 1; j < 14; j++) {
            NameRel6[j] = c[j];
            Nop();
            Nop();
            Nop();
            Nop();
        }
    }
    NameRel6[14] = 0;

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_NameRel7, c, 0x10);
    NameRel7[0] = c[0];
    if (NameRel7[0] == 0xFF) {
        NameRel7[0] = 'R';
        NameRel7[1] = 'E';
        NameRel7[2] = 'L';
        NameRel7[3] = 'E';
        NameRel7[4] = '7';
        NameRel7[5] = 0;
    } else {
        for (j = 1; j < 14; j++) {
            NameRel7[j] = c[j];
            Nop();
            Nop();
            Nop();
            Nop();
        }
    }
    NameRel7[14] = 0;

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_NameRel8, c, 0x10);
    NameRel8[0] = c[0];
    if (NameRel8[0] == 0xFF) {
        NameRel8[0] = 'R';
        NameRel8[1] = 'E';
        NameRel8[2] = 'L';
        NameRel8[3] = 'E';
        NameRel8[4] = '8';
        NameRel8[5] = 0;
    } else {
        for (j = 1; j < 14; j++) {
            NameRel8[j] = c[j];
            Nop();
            Nop();
            Nop();
            Nop();
        }
    }
    NameRel8[14] = 0;

#if defined(USE_LCD)
    // Initialize and display the stack version on the LCD
    LCDInit();
    DelayMs(100);
    strcpypgm2ram((char*) LCDText, "TCPStack " TCPIP_STACK_VERSION "  "
            "                ");
    LCDUpdate();
#endif

    // Initialize stack-related hardware components that may be
    // required by the UART configuration routines
    TickInit();
#if defined(STACK_USE_MPFS) || defined(STACK_USE_MPFS2)
    MPFSInit();
#endif

    // Initialize Stack and application related NV variables into AppConfig.
    InitAppConfig();

    // Initiates board setup process if button is depressed 
    // on startup
    if (PULSANTE_IO == 0u) {
#if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS)
        // Invalidate the EEPROM contents if BUTTON0 is held down for more than 4 seconds
        DWORD StartTime = TickGet();
        // LED_PUT(0x00);

        while (PULSANTE_IO == 0u) {
            if (TickGet() - StartTime > 4 * TICK_SECOND) {
#if defined(EEPROM_CS_TRIS)
                XEEBeginWrite(0x0000);
                XEEWrite(0xFF);
                XEEEndWrite();
#elif defined(SPIFLASH_CS_TRIS)
                SPIFlashBeginWrite(0x0000);
                SPIFlashWrite(0xFF);
#endif

#if defined(STACK_USE_UART)
                putrsUART("\r\n\r\nBUTTON0 held for more than 4 seconds.  Default settings restored.\r\n\r\n");
#endif

                // LED_PUT(0x0F);
                while ((LONG) (TickGet() - StartTime) <= (LONG) (9 * TICK_SECOND / 2));
                // LED_PUT(0x00);
                LED0_IO ^= 1;
                while (PULSANTE_IO == 0u);
                Reset();
                break;
            }
        }
#endif

#if defined(STACK_USE_UART)
        DoUARTConfig();
#endif
    }

    // Initialize core stack layers (MAC, ARP, TCP, UDP) and
    // application modules (HTTP, SNMP, etc.)
    StackInit();

    // Initialize any application-specific modules or functions/
    // For this demo application, this only includes the
    // UART 2 TCP Bridge
#if defined(STACK_USE_UART2TCP_BRIDGE)
    UART2TCPBridgeInit();
#endif

#if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
    ZeroconfLLInitialize();
#endif

#if defined(STACK_USE_ZEROCONF_MDNS_SD)
    mDNSInitialize(MY_DEFAULT_HOST_NAME);
    mDNSServiceRegister(
            (const char *) "DemoWebServer", // base name of the service
            "_http._tcp.local", // type of the service
            80, // TCP or UDP port, at which this service is available
            ((const BYTE *) "path=/index.htm"), // TXT info
            1, // auto rename the service when if needed
            NULL, // no callback function
            NULL // no application context
            );

    mDNSMulticastFilterRegister();
#endif

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_MappaIn, c, 0x10);
    MappaIn1 = c[0];
    if (MappaIn1 > 34) {
        MappaIn1 = 1;
    }
    Nop();
    Nop();
    Nop();
    Nop();
    MappaIn2 = c[1];
    if (MappaIn2 > 34) {
        MappaIn2 = 2;
    }
    Nop();
    Nop();
    Nop();
    Nop();
    MappaIn3 = c[2];
    if (MappaIn3 > 34) {
        MappaIn3 = 3;
    }
    Nop();
    Nop();
    Nop();
    Nop();
    MappaIn4 = c[3];
    if (MappaIn4 > 34) {
        MappaIn4 = 4;
    }
    Nop();
    Nop();
    Nop();
    Nop();
    MappaIn5 = c[4];
    if (MappaIn5 > 34) {
        MappaIn5 = 5;
    }
    Nop();
    Nop();
    Nop();
    Nop();
    MappaIn6 = c[5];
    if (MappaIn6 > 34) {
        MappaIn6 = 6;
    }
    Nop();
    Nop();
    Nop();
    Nop();
    MappaIn7 = c[6];
    if (MappaIn7 > 34) {
        MappaIn7 = 7;
    }
    Nop();
    Nop();
    Nop();
    Nop();
    MappaIn8 = c[7];
    if (MappaIn8 > 34) {
        MappaIn8 = 8;
    }
    Nop();
    Nop();
    Nop();
    Nop();

    ///////////////////////////////////////////////
    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_DurataRele_part1, c, 0x10);
    DurataRele1[0] = c[0];
    DurataRele1[1] = c[1];
    DurataRele1[2] = c[2];
    DurataRele1[3] = c[3];
    FixDurataRele1 = ((DurataRele1[0]) - 48u)*1000u + ((DurataRele1[1]) - 48u)*100u + ((DurataRele1[2]) - 48u)*10u + ((DurataRele1[3]) - 48u);

    DurataRele2[0] = c[4];
    DurataRele2[1] = c[5];
    DurataRele2[2] = c[6];
    DurataRele2[3] = c[7];
    FixDurataRele2 = ((DurataRele2[0]) - 48u)*1000u + ((DurataRele2[1]) - 48u)*100u + ((DurataRele2[2]) - 48u)*10u + ((DurataRele2[3]) - 48u);

    DurataRele3[0] = c[8];
    DurataRele3[1] = c[9];
    DurataRele3[2] = c[10];
    DurataRele3[3] = c[11];
    FixDurataRele3 = ((DurataRele3[0]) - 48u)*1000u + ((DurataRele3[1]) - 48u)*100u + ((DurataRele3[2]) - 48u)*10u + ((DurataRele3[3]) - 48u);

    DurataRele4[0] = c[12];
    DurataRele4[1] = c[13];
    DurataRele4[2] = c[14];
    DurataRele4[3] = c[15];
    FixDurataRele4 = ((DurataRele4[0]) - 48u)*1000u + ((DurataRele4[1]) - 48u)*100u + ((DurataRele4[2]) - 48u)*10u + ((DurataRele4[3]) - 48u);

    p_c = c;
    clear(p_c);
    XEEReadArray(extEEaddr_DurataRele_part2, c, 0x10);
    DurataRele5[0] = c[0];
    DurataRele5[1] = c[1];
    DurataRele5[2] = c[2];
    DurataRele5[3] = c[3];
    FixDurataRele5 = ((DurataRele5[0]) - 48u)*1000u + ((DurataRele5[1]) - 48u)*100u + ((DurataRele5[2]) - 48u)*10u + ((DurataRele5[3]) - 48u);

    DurataRele6[0] = c[4];
    DurataRele6[1] = c[5];
    DurataRele6[2] = c[6];
    DurataRele6[3] = c[7];
    FixDurataRele6 = ((DurataRele6[0]) - 48u)*1000u + ((DurataRele6[1]) - 48u)*100u + ((DurataRele6[2]) - 48u)*10u + ((DurataRele6[3]) - 48u);

    DurataRele7[0] = c[8];
    DurataRele7[1] = c[9];
    DurataRele7[2] = c[10];
    DurataRele7[3] = c[11];
    FixDurataRele7 = ((DurataRele7[0]) - 48u)*1000u + ((DurataRele7[1]) - 48u)*100u + ((DurataRele7[2]) - 48u)*10u + ((DurataRele7[3]) - 48u);

    DurataRele8[0] = c[12];
    DurataRele8[1] = c[13];
    DurataRele8[2] = c[14];
    DurataRele8[3] = c[15];
    FixDurataRele8 = ((DurataRele8[0]) - 48u)*1000u + ((DurataRele8[1]) - 48u)*100u + ((DurataRele8[2]) - 48u)*10u + ((DurataRele8[3]) - 48u);

    ///////////////////////////////////////////////

    // Now that all items are initialized, begin the co-operative
    // multitasking loop.  This infinite loop will continuously
    // execute all stack-related tasks, as well as your own
    // application's functions.  Custom functions should be added
    // at the end of this loop.
    // Note that this is a "co-operative mult-tasking" mechanism
    // where every task performs its tasks (whether all in one shot
    // or part of it) and returns so that other tasks can do their
    // job.
    // If a task needs very long time to do its job, it must be broken
    // down into smaller pieces so that other tasks can have CPU time.
    while (1) {
        if (INPUT1_IO == 0) {
            if (CntFiltroIn1 < 10) {
                CntFiltroIn1++;
            }
        } else {
            if (CntFiltroIn1 > 0) {
                CntFiltroIn1--;
            } else {
                ComandoRele1Eseguito = FALSE;
                ComandoRele_1Eseguito = FALSE;
            }
        }
        if ((CntFiltroIn1 >= 10) && (ComandoRele1Eseguito == FALSE)) {
            if (MappaIn1 == 1) {
                RELAY1_IO = !RELAY1_IO;
            } else if (MappaIn1 == 2) {
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn1 == 3) {
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn1 == 4) {
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn1 == 5) {
                RELAY5_IO = !RELAY5_IO;
            } else if (MappaIn1 == 6) {
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn1 == 7) {
                RELAY7_IO = !RELAY7_IO;
            } else if (MappaIn1 == 8) {
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn1 == 9) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
            } else if (MappaIn1 == 10) {
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            } else if (MappaIn1 == 11) {
                CntDurataRele3 = 0;
                RELAY3_IO = 1;
            } else if (MappaIn1 == 12) {
                CntDurataRele4 = 0;
                RELAY4_IO = 1;
            } else if (MappaIn1 == 13) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
            } else if (MappaIn1 == 14) {
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
            } else if (MappaIn1 == 15) {
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
            } else if (MappaIn1 == 16) {
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            } else if (MappaIn1 == 17) {
                RELAY1_IO = 1;
                Nop();
                Nop();
                RELAY2_IO = 1;
                Nop();
                Nop();
                RELAY3_IO = 1;
                Nop();
                Nop();
                RELAY4_IO = 1;
                Nop();
                Nop();
                RELAY5_IO = 1;
                Nop();
                Nop();
                RELAY6_IO = 1;
                Nop();
                Nop();
                RELAY7_IO = 1;
                Nop();
                Nop();
                RELAY8_IO = 1;
            } else if (MappaIn1 == 18) {
                RELAY1_IO = 0;
                Nop();
                Nop();
                RELAY2_IO = 0;
                Nop();
                Nop();
                RELAY3_IO = 0;
                Nop();
                Nop();
                RELAY4_IO = 0;
                Nop();
                Nop();
                RELAY5_IO = 0;
                Nop();
                Nop();
                RELAY6_IO = 0;
                Nop();
                Nop();
                RELAY7_IO = 0;
                Nop();
                Nop();
                RELAY8_IO = 0;
            } else if (MappaIn1 == 19) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn1 == 20) {
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn1 == 21) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn1 == 22) {
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn1 == 23) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn1 == 24) {
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn1 == 25) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn1 == 26) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn1 == 27) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            }//else if (MappaIn1 == 28) { CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
                //else if (MappaIn1 == 29) { CntDurataRele5 = 0; RELAY5_IO = 1; CntDurataRele6 = 0; RELAY6_IO = 1;}
                //else if (MappaIn1 == 30) { CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn1 == 31) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1;}
                //else if (MappaIn1 == 32) { CntDurataRele6 = 0; RELAY6_IO = 1; CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn1 == 33) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
            else if (MappaIn1 == 34) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            }
            ComandoRele1Eseguito = TRUE;
        }
        // ----------
        if (INPUT2_IO == 0) {
            if (CntFiltroIn2 < 10) {
                CntFiltroIn2++;
            }
        } else {
            if (CntFiltroIn2 > 0) {
                CntFiltroIn2--;
            } else {
                ComandoRele2Eseguito = FALSE;
                ComandoRele_2Eseguito = FALSE;
            }
        }
        if ((CntFiltroIn2 >= 10) && (ComandoRele2Eseguito == FALSE)) {
            if (MappaIn2 == 1) {
                RELAY1_IO = !RELAY1_IO;
            } else if (MappaIn2 == 2) {
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn2 == 3) {
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn2 == 4) {
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn2 == 5) {
                RELAY5_IO = !RELAY5_IO;
            } else if (MappaIn2 == 6) {
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn2 == 7) {
                RELAY7_IO = !RELAY7_IO;
            } else if (MappaIn2 == 8) {
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn2 == 9) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
            } else if (MappaIn2 == 10) {
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            } else if (MappaIn2 == 11) {
                CntDurataRele3 = 0;
                RELAY3_IO = 1;
            } else if (MappaIn2 == 12) {
                CntDurataRele4 = 0;
                RELAY4_IO = 1;
            } else if (MappaIn2 == 13) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
            } else if (MappaIn2 == 14) {
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
            } else if (MappaIn2 == 15) {
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
            } else if (MappaIn2 == 16) {
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            } else if (MappaIn2 == 17) {
                RELAY1_IO = 1;
                Nop();
                Nop();
                RELAY2_IO = 1;
                Nop();
                Nop();
                RELAY3_IO = 1;
                Nop();
                Nop();
                RELAY4_IO = 1;
                Nop();
                Nop();
                RELAY5_IO = 1;
                Nop();
                Nop();
                RELAY6_IO = 1;
                Nop();
                Nop();
                RELAY7_IO = 1;
                Nop();
                Nop();
                RELAY8_IO = 1;
            } else if (MappaIn2 == 18) {
                RELAY1_IO = 0;
                Nop();
                Nop();
                RELAY2_IO = 0;
                Nop();
                Nop();
                RELAY3_IO = 0;
                Nop();
                Nop();
                RELAY4_IO = 0;
                Nop();
                Nop();
                RELAY5_IO = 0;
                Nop();
                Nop();
                RELAY6_IO = 0;
                Nop();
                Nop();
                RELAY7_IO = 0;
                Nop();
                Nop();
                RELAY8_IO = 0;
            } else if (MappaIn2 == 19) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn2 == 20) {
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn2 == 21) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn2 == 22) {
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn2 == 23) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn2 == 24) {
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn2 == 25) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn2 == 26) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn2 == 27) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            }//else if (MappaIn2 == 28) { CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
                //else if (MappaIn2 == 29) { CntDurataRele5 = 0; RELAY5_IO = 1; CntDurataRele6 = 0; RELAY6_IO = 1;}
                //else if (MappaIn2 == 30) { CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn2 == 31) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1;}
                //else if (MappaIn2 == 32) { CntDurataRele6 = 0; RELAY6_IO = 1; CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn2 == 33) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
            else if (MappaIn2 == 34) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            }
            ComandoRele2Eseguito = TRUE;
        }
        // ----------
        if (INPUT3_IO == 0) {
            if (CntFiltroIn3 < 10) {
                CntFiltroIn3++;
            }
        } else {
            if (CntFiltroIn3 > 0) {
                CntFiltroIn3--;
            } else {
                ComandoRele3Eseguito = FALSE;
                ComandoRele_3Eseguito = FALSE;
            }
        }
        if ((CntFiltroIn3 >= 10) && (ComandoRele3Eseguito == FALSE)) {
            if (MappaIn3 == 1) {
                RELAY1_IO = !RELAY1_IO;
            } else if (MappaIn3 == 2) {
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn3 == 3) {
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn3 == 4) {
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn3 == 5) {
                RELAY5_IO = !RELAY5_IO;
            } else if (MappaIn3 == 6) {
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn3 == 7) {
                RELAY7_IO = !RELAY7_IO;
            } else if (MappaIn3 == 8) {
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn3 == 9) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
            } else if (MappaIn3 == 10) {
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            } else if (MappaIn3 == 11) {
                CntDurataRele3 = 0;
                RELAY3_IO = 1;
            } else if (MappaIn3 == 12) {
                CntDurataRele4 = 0;
                RELAY4_IO = 1;
            } else if (MappaIn3 == 13) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
            } else if (MappaIn3 == 14) {
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
            } else if (MappaIn3 == 15) {
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
            } else if (MappaIn3 == 16) {
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            } else if (MappaIn3 == 17) {
                RELAY1_IO = 1;
                Nop();
                Nop();
                RELAY2_IO = 1;
                Nop();
                Nop();
                RELAY3_IO = 1;
                Nop();
                Nop();
                RELAY4_IO = 1;
                Nop();
                Nop();
                RELAY5_IO = 1;
                Nop();
                Nop();
                RELAY6_IO = 1;
                Nop();
                Nop();
                RELAY7_IO = 1;
                Nop();
                Nop();
                RELAY8_IO = 1;
            } else if (MappaIn3 == 18) {
                RELAY1_IO = 0;
                Nop();
                Nop();
                RELAY2_IO = 0;
                Nop();
                Nop();
                RELAY3_IO = 0;
                Nop();
                Nop();
                RELAY4_IO = 0;
                Nop();
                Nop();
                RELAY5_IO = 0;
                Nop();
                Nop();
                RELAY6_IO = 0;
                Nop();
                Nop();
                RELAY7_IO = 0;
                Nop();
                Nop();
                RELAY8_IO = 0;
            } else if (MappaIn3 == 19) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn3 == 20) {
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn3 == 21) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn3 == 22) {
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn3 == 23) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn3 == 24) {
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn3 == 25) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn3 == 26) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn3 == 27) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            }//else if (MappaIn3 == 28) { CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
                //else if (MappaIn3 == 29) { CntDurataRele5 = 0; RELAY5_IO = 1; CntDurataRele6 = 0; RELAY6_IO = 1;}
                //else if (MappaIn3 == 30) { CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn3 == 31) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1;}
                //else if (MappaIn3 == 32) { CntDurataRele6 = 0; RELAY6_IO = 1; CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn3 == 33) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
            else if (MappaIn3 == 34) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            }
            ComandoRele3Eseguito = TRUE;
        }
        // ----------
        if (INPUT4_IO == 0) {
            if (CntFiltroIn4 < 10) {
                CntFiltroIn4++;
            }
        } else {
            if (CntFiltroIn4 > 0) {
                CntFiltroIn4--;
            } else {
                ComandoRele4Eseguito = FALSE;
                ComandoRele_4Eseguito = FALSE;
            }
        }
        if ((CntFiltroIn4 >= 10) && (ComandoRele4Eseguito == FALSE)) {
            if (MappaIn4 == 1) {
                RELAY1_IO = !RELAY1_IO;
            } else if (MappaIn4 == 2) {
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn4 == 3) {
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn4 == 4) {
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn4 == 5) {
                RELAY5_IO = !RELAY5_IO;
            } else if (MappaIn4 == 6) {
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn4 == 7) {
                RELAY7_IO = !RELAY7_IO;
            } else if (MappaIn4 == 8) {
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn4 == 9) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
            } else if (MappaIn4 == 10) {
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            } else if (MappaIn4 == 11) {
                CntDurataRele3 = 0;
                RELAY3_IO = 1;
            } else if (MappaIn4 == 12) {
                CntDurataRele4 = 0;
                RELAY4_IO = 1;
            } else if (MappaIn4 == 13) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
            } else if (MappaIn4 == 14) {
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
            } else if (MappaIn4 == 15) {
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
            } else if (MappaIn4 == 16) {
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            } else if (MappaIn4 == 17) {
                RELAY1_IO = 1;
                Nop();
                Nop();
                RELAY2_IO = 1;
                Nop();
                Nop();
                RELAY3_IO = 1;
                Nop();
                Nop();
                RELAY4_IO = 1;
                Nop();
                Nop();
                RELAY5_IO = 1;
                Nop();
                Nop();
                RELAY6_IO = 1;
                Nop();
                Nop();
                RELAY7_IO = 1;
                Nop();
                Nop();
                RELAY8_IO = 1;
            } else if (MappaIn4 == 18) {
                RELAY1_IO = 0;
                Nop();
                Nop();
                RELAY2_IO = 0;
                Nop();
                Nop();
                RELAY3_IO = 0;
                Nop();
                Nop();
                RELAY4_IO = 0;
                Nop();
                Nop();
                RELAY5_IO = 0;
                Nop();
                Nop();
                RELAY6_IO = 0;
                Nop();
                Nop();
                RELAY7_IO = 0;
                Nop();
                Nop();
                RELAY8_IO = 0;
            } else if (MappaIn4 == 19) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn4 == 20) {
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn4 == 21) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn4 == 22) {
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn4 == 23) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn4 == 24) {
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn4 == 25) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn4 == 26) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn4 == 27) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            }//else if (MappaIn4 == 28) { CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
                //else if (MappaIn4 == 29) { CntDurataRele5 = 0; RELAY5_IO = 1; CntDurataRele6 = 0; RELAY6_IO = 1;}
                //else if (MappaIn4 == 30) { CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn4 == 31) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1;}
                //else if (MappaIn4 == 32) { CntDurataRele6 = 0; RELAY6_IO = 1; CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn4 == 33) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
            else if (MappaIn4 == 34) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            }
            ComandoRele4Eseguito = TRUE;
        }
        // ----------
        if (INPUT5_IO == 0) {
            if (CntFiltroIn5 < 10) {
                CntFiltroIn5++;
            }
        } else {
            if (CntFiltroIn5 > 0) {
                CntFiltroIn5--;
            } else {
                ComandoRele5Eseguito = FALSE;
                ComandoRele_5Eseguito = FALSE;
            }
        }
        if ((CntFiltroIn5 >= 10) && (ComandoRele5Eseguito == FALSE)) {
            if (MappaIn5 == 1) {
                RELAY1_IO = !RELAY1_IO;
            } else if (MappaIn5 == 2) {
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn5 == 3) {
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn5 == 4) {
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn5 == 5) {
                RELAY5_IO = !RELAY5_IO;
            } else if (MappaIn5 == 6) {
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn5 == 7) {
                RELAY7_IO = !RELAY7_IO;
            } else if (MappaIn5 == 8) {
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn5 == 9) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
            } else if (MappaIn5 == 10) {
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            } else if (MappaIn5 == 11) {
                CntDurataRele3 = 0;
                RELAY3_IO = 1;
            } else if (MappaIn5 == 12) {
                CntDurataRele4 = 0;
                RELAY4_IO = 1;
            } else if (MappaIn5 == 13) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
            } else if (MappaIn5 == 14) {
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
            } else if (MappaIn5 == 15) {
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
            } else if (MappaIn5 == 16) {
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            } else if (MappaIn5 == 17) {
                RELAY1_IO = 1;
                Nop();
                Nop();
                RELAY2_IO = 1;
                Nop();
                Nop();
                RELAY3_IO = 1;
                Nop();
                Nop();
                RELAY4_IO = 1;
                Nop();
                Nop();
                RELAY5_IO = 1;
                Nop();
                Nop();
                RELAY6_IO = 1;
                Nop();
                Nop();
                RELAY7_IO = 1;
                Nop();
                Nop();
                RELAY8_IO = 1;
            } else if (MappaIn5 == 18) {
                RELAY1_IO = 0;
                Nop();
                Nop();
                RELAY2_IO = 0;
                Nop();
                Nop();
                RELAY3_IO = 0;
                Nop();
                Nop();
                RELAY4_IO = 0;
                Nop();
                Nop();
                RELAY5_IO = 0;
                Nop();
                Nop();
                RELAY6_IO = 0;
                Nop();
                Nop();
                RELAY7_IO = 0;
                Nop();
                Nop();
                RELAY8_IO = 0;
            } else if (MappaIn5 == 19) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn5 == 20) {
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn5 == 21) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn5 == 22) {
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn5 == 23) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn5 == 24) {
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn5 == 25) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn5 == 26) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn5 == 27) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            }//else if (MappaIn5 == 28) { CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
                //else if (MappaIn5 == 29) { CntDurataRele5 = 0; RELAY5_IO = 1; CntDurataRele6 = 0; RELAY6_IO = 1;}
                //else if (MappaIn5 == 30) { CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn5 == 31) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1;}
                //else if (MappaIn5 == 32) { CntDurataRele6 = 0; RELAY6_IO = 1; CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn5 == 33) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
            else if (MappaIn5 == 34) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            }
            ComandoRele5Eseguito = TRUE;
        }
        // ----------
        if (INPUT6_IO == 0) {
            if (CntFiltroIn6 < 10) {
                CntFiltroIn6++;
            }
        } else {
            if (CntFiltroIn6 > 0) {
                CntFiltroIn6--;
            } else {
                ComandoRele6Eseguito = FALSE;
                ComandoRele_6Eseguito = FALSE;
            }
        }
        if ((CntFiltroIn6 >= 10) && (ComandoRele6Eseguito == FALSE)) {
            if (MappaIn6 == 1) {
                RELAY1_IO = !RELAY1_IO;
            } else if (MappaIn6 == 2) {
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn6 == 3) {
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn6 == 4) {
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn6 == 5) {
                RELAY5_IO = !RELAY5_IO;
            } else if (MappaIn6 == 6) {
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn6 == 7) {
                RELAY7_IO = !RELAY7_IO;
            } else if (MappaIn6 == 8) {
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn6 == 9) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
            } else if (MappaIn6 == 10) {
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            } else if (MappaIn6 == 11) {
                CntDurataRele3 = 0;
                RELAY3_IO = 1;
            } else if (MappaIn6 == 12) {
                CntDurataRele4 = 0;
                RELAY4_IO = 1;
            } else if (MappaIn6 == 13) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
            } else if (MappaIn6 == 14) {
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
            } else if (MappaIn6 == 15) {
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
            } else if (MappaIn6 == 16) {
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            } else if (MappaIn6 == 17) {
                RELAY1_IO = 1;
                Nop();
                Nop();
                RELAY2_IO = 1;
                Nop();
                Nop();
                RELAY3_IO = 1;
                Nop();
                Nop();
                RELAY4_IO = 1;
                Nop();
                Nop();
                RELAY5_IO = 1;
                Nop();
                Nop();
                RELAY6_IO = 1;
                Nop();
                Nop();
                RELAY7_IO = 1;
                Nop();
                Nop();
                RELAY8_IO = 1;
            } else if (MappaIn6 == 18) {
                RELAY1_IO = 0;
                Nop();
                Nop();
                RELAY2_IO = 0;
                Nop();
                Nop();
                RELAY3_IO = 0;
                Nop();
                Nop();
                RELAY4_IO = 0;
                Nop();
                Nop();
                RELAY5_IO = 0;
                Nop();
                Nop();
                RELAY6_IO = 0;
                Nop();
                Nop();
                RELAY7_IO = 0;
                Nop();
                Nop();
                RELAY8_IO = 0;
            } else if (MappaIn6 == 19) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn6 == 20) {
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn6 == 21) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn6 == 22) {
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn6 == 23) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn6 == 24) {
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn6 == 25) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn6 == 26) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn6 == 27) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            }//else if (MappaIn6 == 28) { CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
                //else if (MappaIn6 == 29) { CntDurataRele5 = 0; RELAY5_IO = 1; CntDurataRele6 = 0; RELAY6_IO = 1;}
                //else if (MappaIn6 == 30) { CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn6 == 31) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1;}
                //else if (MappaIn6 == 32) { CntDurataRele6 = 0; RELAY6_IO = 1; CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn6 == 33) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
            else if (MappaIn6 == 34) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            }
            ComandoRele6Eseguito = TRUE;
        }
        // ----------
        if (INPUT7_IO == 0) {
            if (CntFiltroIn7 < 10) {
                CntFiltroIn7++;
            }
        } else {
            if (CntFiltroIn7 > 0) {
                CntFiltroIn7--;
            } else {
                ComandoRele7Eseguito = FALSE;
                ComandoRele_7Eseguito = FALSE;
            }
        }
        if ((CntFiltroIn7 >= 10) && (ComandoRele7Eseguito == FALSE)) {
            if (MappaIn7 == 1) {
                RELAY1_IO = !RELAY1_IO;
            } else if (MappaIn7 == 2) {
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn7 == 3) {
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn7 == 4) {
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn7 == 5) {
                RELAY5_IO = !RELAY5_IO;
            } else if (MappaIn7 == 6) {
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn7 == 7) {
                RELAY7_IO = !RELAY7_IO;
            } else if (MappaIn7 == 8) {
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn7 == 9) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
            } else if (MappaIn7 == 10) {
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            } else if (MappaIn7 == 11) {
                CntDurataRele3 = 0;
                RELAY3_IO = 1;
            } else if (MappaIn7 == 12) {
                CntDurataRele4 = 0;
                RELAY4_IO = 1;
            } else if (MappaIn7 == 13) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
            } else if (MappaIn7 == 14) {
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
            } else if (MappaIn7 == 15) {
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
            } else if (MappaIn7 == 16) {
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            } else if (MappaIn7 == 17) {
                RELAY1_IO = 1;
                Nop();
                Nop();
                RELAY2_IO = 1;
                Nop();
                Nop();
                RELAY3_IO = 1;
                Nop();
                Nop();
                RELAY4_IO = 1;
                Nop();
                Nop();
                RELAY5_IO = 1;
                Nop();
                Nop();
                RELAY6_IO = 1;
                Nop();
                Nop();
                RELAY7_IO = 1;
                Nop();
                Nop();
                RELAY8_IO = 1;
            } else if (MappaIn7 == 18) {
                RELAY1_IO = 0;
                Nop();
                Nop();
                RELAY2_IO = 0;
                Nop();
                Nop();
                RELAY3_IO = 0;
                Nop();
                Nop();
                RELAY4_IO = 0;
                Nop();
                Nop();
                RELAY5_IO = 0;
                Nop();
                Nop();
                RELAY6_IO = 0;
                Nop();
                Nop();
                RELAY7_IO = 0;
                Nop();
                Nop();
                RELAY8_IO = 0;
            } else if (MappaIn7 == 19) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn7 == 20) {
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn7 == 21) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn7 == 22) {
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn7 == 23) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn7 == 24) {
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn7 == 25) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn7 == 26) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn7 == 27) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            }//else if (MappaIn7 == 28) { CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
                //else if (MappaIn7 == 29) { CntDurataRele5 = 0; RELAY5_IO = 1; CntDurataRele6 = 0; RELAY6_IO = 1;}
                //else if (MappaIn7 == 30) { CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn7 == 31) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1;}
                //else if (MappaIn7 == 32) { CntDurataRele6 = 0; RELAY6_IO = 1; CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn7 == 33) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
            else if (MappaIn7 == 34) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            }
            ComandoRele7Eseguito = TRUE;
        }
        // ----------
        if (INPUT8_IO == 0) {
            if (CntFiltroIn8 < 10) {
                CntFiltroIn8++;
            }
        } else {
            if (CntFiltroIn8 > 0) {
                CntFiltroIn8--;
            } else {
                ComandoRele8Eseguito = FALSE;
                ComandoRele_8Eseguito = FALSE;
            }
        }
        if ((CntFiltroIn8 >= 10) && (ComandoRele8Eseguito == FALSE)) {
            if (MappaIn8 == 1) {
                RELAY1_IO = !RELAY1_IO;
            } else if (MappaIn8 == 2) {
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn8 == 3) {
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn8 == 4) {
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn8 == 5) {
                RELAY5_IO = !RELAY5_IO;
            } else if (MappaIn8 == 6) {
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn8 == 7) {
                RELAY7_IO = !RELAY7_IO;
            } else if (MappaIn8 == 8) {
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn8 == 9) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
            } else if (MappaIn8 == 10) {
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            } else if (MappaIn8 == 11) {
                CntDurataRele3 = 0;
                RELAY3_IO = 1;
            } else if (MappaIn8 == 12) {
                CntDurataRele4 = 0;
                RELAY4_IO = 1;
            } else if (MappaIn8 == 13) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
            } else if (MappaIn8 == 14) {
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
            } else if (MappaIn8 == 15) {
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
            } else if (MappaIn8 == 16) {
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            } else if (MappaIn8 == 17) {
                RELAY1_IO = 1;
                Nop();
                Nop();
                RELAY2_IO = 1;
                Nop();
                Nop();
                RELAY3_IO = 1;
                Nop();
                Nop();
                RELAY4_IO = 1;
                Nop();
                Nop();
                RELAY5_IO = 1;
                Nop();
                Nop();
                RELAY6_IO = 1;
                Nop();
                Nop();
                RELAY7_IO = 1;
                Nop();
                Nop();
                RELAY8_IO = 1;
            } else if (MappaIn8 == 18) {
                RELAY1_IO = 0;
                Nop();
                Nop();
                RELAY2_IO = 0;
                Nop();
                Nop();
                RELAY3_IO = 0;
                Nop();
                Nop();
                RELAY4_IO = 0;
                Nop();
                Nop();
                RELAY5_IO = 0;
                Nop();
                Nop();
                RELAY6_IO = 0;
                Nop();
                Nop();
                RELAY7_IO = 0;
                Nop();
                Nop();
                RELAY8_IO = 0;
            } else if (MappaIn8 == 19) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
            } else if (MappaIn8 == 20) {
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn8 == 21) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
            } else if (MappaIn8 == 22) {
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn8 == 23) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
            } else if (MappaIn8 == 24) {
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn8 == 25) {
                RELAY1_IO = !RELAY1_IO;
                RELAY2_IO = !RELAY2_IO;
                RELAY3_IO = !RELAY3_IO;
                RELAY4_IO = !RELAY4_IO;
            } else if (MappaIn8 == 26) {
                RELAY5_IO = !RELAY5_IO;
                RELAY6_IO = !RELAY6_IO;
                RELAY7_IO = !RELAY7_IO;
                RELAY8_IO = !RELAY8_IO;
            } else if (MappaIn8 == 27) {
                CntDurataRele1 = 0;
                RELAY1_IO = 1;
                CntDurataRele2 = 0;
                RELAY2_IO = 1;
            }//else if (MappaIn8 == 28) { CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
                //else if (MappaIn8 == 29) { CntDurataRele5 = 0; RELAY5_IO = 1; CntDurataRele6 = 0; RELAY6_IO = 1;}
                //else if (MappaIn8 == 30) { CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn8 == 31) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1;}
                //else if (MappaIn8 == 32) { CntDurataRele6 = 0; RELAY6_IO = 1; CntDurataRele7 = 0; RELAY7_IO = 1; CntDurataRele8 = 0; RELAY8_IO = 1;}
                //else if (MappaIn8 == 33) { CntDurataRele1 = 0; RELAY1_IO = 1; CntDurataRele2 = 0; RELAY2_IO = 1; CntDurataRele3 = 0; RELAY3_IO = 1; CntDurataRele4 = 0; RELAY4_IO = 1;}
            else if (MappaIn8 == 34) {
                CntDurataRele5 = 0;
                RELAY5_IO = 1;
                CntDurataRele6 = 0;
                RELAY6_IO = 1;
                CntDurataRele7 = 0;
                RELAY7_IO = 1;
                CntDurataRele8 = 0;
                RELAY8_IO = 1;
            }
            ComandoRele8Eseguito = TRUE;
        }
        // } fine if soloFW soloSW entrambi

        if (TickGet() - t_lampeggio >= (TICK_SECOND * 2)) {
            t_lampeggio = TickGet();
            //LED_ROSSO_IO ^= 1;

            //if (CntDurataRele1 == 0) {RELAY1_IO = 1;}
            if (CntDurataRele1 < FixDurataRele1) {
                CntDurataRele1++;
            }
            if (CntDurataRele1 == FixDurataRele1) {
                RELAY1_IO = 0;
                CntDurataRele1++;
            }
            // fine Cnt Rele 1
            //if (CntDurataRele2 == 0) {RELAY2_IO = 1;}
            if (CntDurataRele2 < FixDurataRele2) {
                CntDurataRele2++;
            }
            if (CntDurataRele2 == FixDurataRele2) {
                RELAY2_IO = 0;
                CntDurataRele2++;
            }
            // fine Cnt Rele 2
            //if (CntDurataRele3 == 0) {RELAY3_IO = 1;}
            if (CntDurataRele3 < FixDurataRele3) {
                CntDurataRele3++;
            }
            if (CntDurataRele3 == FixDurataRele3) {
                RELAY3_IO = 0;
                CntDurataRele3++;
            }
            // fine Cnt Rele 3
            //if (CntDurataRele4 == 0) {RELAY4_IO = 1;}
            if (CntDurataRele4 < FixDurataRele4) {
                CntDurataRele4++;
            }
            if (CntDurataRele4 == FixDurataRele4) {
                RELAY4_IO = 0;
                CntDurataRele4++;
            }
            // fine Cnt Rele 4
            //if (CntDurataRele5 == 0) {RELAY5_IO = 1;}
            if (CntDurataRele5 < FixDurataRele5) {
                CntDurataRele5++;
            }
            if (CntDurataRele5 == FixDurataRele5) {
                RELAY5_IO = 0;
                CntDurataRele5++;
            }
            // fine Cnt Rele 5
            //if (CntDurataRele6 == 0) {RELAY6_IO = 1;}
            if (CntDurataRele6 < FixDurataRele6) {
                CntDurataRele6++;
            }
            if (CntDurataRele6 == FixDurataRele6) {
                RELAY6_IO = 0;
                CntDurataRele6++;
            }
            // fine Cnt Rele 6
            //if (CntDurataRele7 == 0) {RELAY7_IO = 1;}
            if (CntDurataRele7 < FixDurataRele7) {
                CntDurataRele7++;
            }
            if (CntDurataRele7 == FixDurataRele7) {
                RELAY7_IO = 0;
                CntDurataRele7++;
            }
            // fine Cnt Rele 7
            //if (CntDurataRele8 == 0) {RELAY8_IO = 1;}
            if (CntDurataRele8 < FixDurataRele8) {
                CntDurataRele8++;
            }
            if (CntDurataRele8 == FixDurataRele8) {
                RELAY8_IO = 0;
                CntDurataRele8++;
            }
            // fine Cnt Rele 8


        }
        /*
        // Blink LED0 (right most one) every second.
        if (TickGet() - t >= TICK_SECOND) {
            t = TickGet();
            LED0_IO ^= 1;

            if (contatore_toggle1 > 0) {
                contatore_toggle1++;
            }
            if (contatore_toggle1 >= 6) {
                RELAY1_IO = 0;
                contatore_toggle1 = 0;
            }
            if (contatore_toggle2 > 0) {
                contatore_toggle2++;
            }
            if (contatore_toggle2 >= 6) {
                RELAY2_IO = 0;
                contatore_toggle2 = 0;
            }
            if (contatore_toggle3 > 0) {
                contatore_toggle3++;
            }
            if (contatore_toggle3 >= 6) {
                RELAY3_IO = 0;
                contatore_toggle3 = 0;
            }
            if (contatore_toggle4 > 0) {
                contatore_toggle4++;
            }
            if (contatore_toggle4 >= 6) {
                RELAY4_IO = 0;
                contatore_toggle4 = 0;
            }
            if (contatore_toggle5 > 0) {
                contatore_toggle5++;
            }
            if (contatore_toggle5 >= 6) {
                RELAY5_IO = 0;
                contatore_toggle5 = 0;
            }
            if (contatore_toggle6 > 0) {
                contatore_toggle6++;
            }
            if (contatore_toggle6 >= 6) {
                RELAY6_IO = 0;
                contatore_toggle6 = 0;
            }
            if (contatore_toggle7 > 0) {
                contatore_toggle7++;
            }
            if (contatore_toggle7 >= 6) {
                RELAY7_IO = 0;
                contatore_toggle7 = 0;
            }
            if (contatore_toggle8 > 0) {
                contatore_toggle8++;
            }
            if (contatore_toggle8 >= 6) {
                RELAY8_IO = 0;
                contatore_toggle8 = 0;
            }
            if (contatore_toggle9 > 0) {
                contatore_toggle9++;
            }
            if (contatore_toggle9 >= 6) {
                RELAY9_IO = 0;
                contatore_toggle9 = 0;
            }
            if (contatore_toggle10 > 0) {
                contatore_toggle10++;
            }
            if (contatore_toggle10 >= 6) {
                RELAY10_IO = 0;
                contatore_toggle10 = 0;
            }
            if (contatore_toggle11 > 0) {
                contatore_toggle11++;
            }
            if (contatore_toggle11 >= 6) {
                RELAY11_IO = 0;
                contatore_toggle11 = 0;
            }
            if (contatore_toggle12 > 0) {
                contatore_toggle12++;
            }
            if (contatore_toggle12 >= 6) {
                RELAY12_IO = 0;
                contatore_toggle12 = 0;
            }
        }
         */

        // alb: INIZIO
        // OK XEEReadArray(0x0000, &c, 0x20);
        /*
        extRom[0]=btohexa_high(*(c[0]));
        extRom[1]=btohexa_low(*(c[0]));
        extRom[2]=' ';
        XEEReadArray(0x0000, c[1], 1);
        extRom[3]=btohexa_high(*(c[1]));
        extRom[4]=btohexa_low(*(c[1]));
        extRom[5]=0;
        extRomAddr[0]='<';
        extRomAddr[1]='b';
        extRomAddr[2]='r';
        extRomAddr[3]='>';
        extRomAddr[4]='1';
        extRomAddr[5]=0;
         */

        // OK sprintf(cc, "%X",c);
        // OK sprintf(cc, "%X",c);
        //printf("aaa");
        //putsUSART(c[0]);
        //for (l = 0; l < 2; l++){
        //    extRom[l]=*c[l+4];
        //}
        // alb: FINE


        // This task performs normal stack task including checking
        // for incoming packet, type of packet and calling
        // appropriate stack entity to process it.
        StackTask();

        // This tasks invokes each of the core stack application tasks
        StackApplications();

#if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
        ZeroconfLLProcess();
#endif

#if defined(STACK_USE_ZEROCONF_MDNS_SD)
        mDNSProcess();
        // Use this function to exercise service update function
        // HTTPUpdateRecord();
#endif

        // Process application specific tasks here.
        // For this demo app, this will include the Generic TCP
        // client and servers, and the SNMP, Ping, and SNMP Trap
        // demos.  Following that, we will process any IO from
        // the inputs on the board itself.
        // Any custom modules or processing you need to do should
        // go here.
#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
        GenericTCPClient();
#endif

#if defined(STACK_USE_GENERIC_TCP_SERVER_EXAMPLE)
        GenericTCPServer();
#endif

#if defined(STACK_USE_SMTP_CLIENT)
        SMTPDemo();
#endif

#if defined(STACK_USE_ICMP_CLIENT)
        PingDemo();
#endif

#if defined(STACK_USE_SNMP_SERVER) && !defined(SNMP_TRAP_DISABLED)
        //User should use one of the following SNMP demo
        // This routine demonstrates V1 or V2 trap formats with one variable binding.
        SNMPTrapDemo();
#if defined(SNMP_STACK_USE_V2_TRAP)
        //This routine provides V2 format notifications with multiple (3) variable bindings
        //User should modify this routine to send v2 trap format notifications with the required varbinds.
        //SNMPV2TrapDemo();
#endif 
        if (gSendTrapFlag)
            SNMPSendTrap();
#endif

#if defined(STACK_USE_BERKELEY_API)
        BerkeleyTCPClientDemo();
        BerkeleyTCPServerDemo();
        BerkeleyUDPClientDemo();
#endif

        // ProcessIO();

        // If the local IP address has changed (ex: due to DHCP lease change)
        // write the new IP address to the LCD display, UART, and Announce 
        // service
        if (dwLastIP != AppConfig.MyIPAddr.Val) {
            dwLastIP = AppConfig.MyIPAddr.Val;

#if defined(STACK_USE_UART)
            putrsUART((ROM char*) "\r\nNew IP Address: ");
#endif

            DisplayIPValue(AppConfig.MyIPAddr);

#if defined(STACK_USE_UART)
            putrsUART((ROM char*) "\r\n");
#endif


#if defined(STACK_USE_ANNOUNCE)
            AnnounceIP();
#endif

#if defined(STACK_USE_ZEROCONF_MDNS_SD)
            mDNSFillHostRecord();
#endif
        }
    }
}

// Writes an IP address to the LCD display and the UART as available

void DisplayIPValue(IP_ADDR IPVal) {
    //	printf("%u.%u.%u.%u", IPVal.v[0], IPVal.v[1], IPVal.v[2], IPVal.v[3]);
    BYTE IPDigit[4];
    BYTE i;
#ifdef USE_LCD
    BYTE j;
    BYTE LCDPos = 16;
#endif

    for (i = 0; i < sizeof (IP_ADDR); i++) {
        uitoa((WORD) IPVal.v[i], IPDigit);

#if defined(STACK_USE_UART)
        putsUART((char *) IPDigit);
#endif

#ifdef USE_LCD
        for (j = 0; j < strlen((char*) IPDigit); j++) {
            LCDText[LCDPos++] = IPDigit[j];
        }
        if (i == sizeof (IP_ADDR) - 1)
            break;
        LCDText[LCDPos++] = '.';
#else
        if (i == sizeof (IP_ADDR) - 1)
            break;
#endif

#if defined(STACK_USE_UART)
        while (BusyUART());
        WriteUART('.');
#endif
    }

#ifdef USE_LCD
    if (LCDPos < 32u)
        LCDText[LCDPos] = 0;
    LCDUpdate();
#endif
}

/* Processes A/D data from the potentiometer
static void ProcessIO(void)
{
#if defined(__C30__) || defined(__C32__)
    // Convert potentiometer result into ASCII string
    uitoa((WORD)ADC1BUF0, AN0String);
#else
    // AN0 should already be set up as an analog input
    ADCON0bits.GO = 1;

    // Wait until A/D conversion is done
    while(ADCON0bits.GO);

        // AD converter errata work around (ex: PIC18F87J10 A2)
        #if !defined(__18F87J50) && !defined(_18F87J50) && !defined(__18F87J11) && !defined(_18F87J11)
                PRODL = ADCON2;
                ADCON2 |= 0x7;	// Select Frc mode by setting ADCS0/ADCS1/ADCS2
                ADCON2 = PRODL;
        #endif

    // Convert 10-bit value into ASCII string
    uitoa(*((WORD*)(&ADRESL)), AN0String);
#endif
}*/

/****************************************************************************
  Function:
    static void InitializeBoard(void)

  Description:
    This routine initializes the hardware.  It is a generic initialization
    routine for many of the Microchip development boards, using definitions
    in HardwareProfile.h to determine specific initialization.

  Precondition:
    None

  Parameters:
    None - None

  Returns:
    None

  Remarks:
    None
 ***************************************************************************/
static void InitializeBoard(void) {
    // LEDs
    LED0_TRIS = 0;

    RELAY1_TRIS = 0;
    RELAY2_TRIS = 0;
    RELAY3_TRIS = 0;
    RELAY4_TRIS = 0;
    RELAY5_TRIS = 0;
    RELAY6_TRIS = 0;
    RELAY7_TRIS = 0;
    RELAY8_TRIS = 0;
    RELAY9_TRIS = 0;
    RELAY10_TRIS = 0;
    RELAY11_TRIS = 0;
    RELAY12_TRIS = 0;

    RELAY1_IO = 0;
    RELAY2_IO = 0;
    RELAY3_IO = 0;
    RELAY4_IO = 0;
    RELAY5_IO = 0;
    RELAY6_IO = 0;
    RELAY7_IO = 0;
    RELAY8_IO = 0;
    RELAY9_IO = 0;
    RELAY10_IO = 0;
    RELAY11_IO = 0;
    RELAY12_IO = 0;

    /*
            OUT13_TRIS = 0;
            OUT14_TRIS = 0;
            OUT15_TRIS = 0;
            OUT16_TRIS = 0;
            OUT17_TRIS = 0;
            OUT18_TRIS = 0;
            OUT19_TRIS = 0;
            OUT20_TRIS = 0;
            OUT21_TRIS = 0;
            OUT22_TRIS = 0;
            OUT23_TRIS = 0;
            OUT24_TRIS = 0;
            OUT25_TRIS = 0;
            OUT26_TRIS = 0;
            OUT27_TRIS = 0;
            OUT28_TRIS = 0;
            OUT29_TRIS = 0;
     */

    PULSANTE_TRIS = 1;
    INPUT1_TRIS = 1;
    INPUT2_TRIS = 1;
    INPUT3_TRIS = 1;
    INPUT4_TRIS = 1;
    INPUT5_TRIS = 1;
    INPUT6_TRIS = 1;
    INPUT7_TRIS = 1;
    INPUT8_TRIS = 1;
    INPUT9_TRIS = 1;
    INPUT10_TRIS = 1;
    INPUT11_TRIS = 1;
    INPUT12_TRIS = 1;

    // LED_PUT(0x00);

#if defined(__18CXX)
    // Enable 4x/5x/96MHz PLL on PIC18F87J10, PIC18F97J60, PIC18F87J50, etc.
    OSCTUNE = 0x40;

    // Set up analog features of PORTA

    // PICDEM.net 2 board has POT on AN2, Temp Sensor on AN3
    //	#if defined(PICDEMNET2)
    //		ADCON0 = 0x09;		// ADON, Channel 2
    //		ADCON1 = 0x0B;		// Vdd/Vss is +/-REF, AN0, AN1, AN2, AN3 are analog
    //	#elif defined(PICDEMZ)
    //		ADCON0 = 0x81;		// ADON, Channel 0, Fosc/32
    ADCON0 = 0x0;
    ADCON1 = 0x0F; // Vdd/Vss is +/-REF, AN0, AN1, AN2, AN3 are all digital
    //	#elif defined(__18F87J11) || defined(_18F87J11) || defined(__18F87J50) || defined(_18F87J50)
    //		ADCON0 = 0x01;		// ADON, Channel 0, Vdd/Vss is +/-REF
    //		WDTCONbits.ADSHR = 1;
    //		ANCON0 = 0xFC;		// AN0 (POT) and AN1 (temp sensor) are anlog
    //		ANCON1 = 0xFF;
    //		WDTCONbits.ADSHR = 0;
    //	#else
    //		ADCON0 = 0x01;		// ADON, Channel 0
    //		ADCON1 = 0x0E;		// Vdd/Vss is +/-REF, AN0 is analog
    //	#endif
    //	ADCON2 = 0xBE;		// Right justify, 20TAD ACQ time, Fosc/64 (~21.0kHz)


    // Enable internal PORTB pull-ups
    INTCON2bits.RBPU = 0;

    // Configure USART
    TXSTA = 0x20;
    RCSTA = 0x90;

    // See if we can use the high baud rate setting
#if ((GetPeripheralClock()+2*BAUD_RATE)/BAUD_RATE/4 - 1) <= 255
    SPBRG = (GetPeripheralClock() + 2 * BAUD_RATE) / BAUD_RATE / 4 - 1;
    TXSTAbits.BRGH = 1;
#else	// Use the low baud rate setting
    SPBRG = (GetPeripheralClock() + 8 * BAUD_RATE) / BAUD_RATE / 16 - 1;
#endif


    // Enable Interrupts
    RCONbits.IPEN = 1; // Enable interrupt priorities
    INTCONbits.GIEH = 1;
    INTCONbits.GIEL = 1;

    // Do a calibration A/D conversion
#if defined(__18F87J10) || defined(__18F86J15) || defined(__18F86J10) || defined(__18F85J15) || defined(__18F85J10) || defined(__18F67J10) || defined(__18F66J15) || defined(__18F66J10) || defined(__18F65J15) || defined(__18F65J10) || defined(__18F97J60) || defined(__18F96J65) || defined(__18F96J60) || defined(__18F87J60) || defined(__18F86J65) || defined(__18F86J60) || defined(__18F67J60) || defined(__18F66J65) || defined(__18F66J60) || \
	     defined(_18F87J10) ||  defined(_18F86J15) || defined(_18F86J10)  ||  defined(_18F85J15) ||  defined(_18F85J10) ||  defined(_18F67J10) ||  defined(_18F66J15) ||  defined(_18F66J10) ||  defined(_18F65J15) ||  defined(_18F65J10) ||  defined(_18F97J60) ||  defined(_18F96J65) ||  defined(_18F96J60) ||  defined(_18F87J60) ||  defined(_18F86J65) ||  defined(_18F86J60) ||  defined(_18F67J60) ||  defined(_18F66J65) ||  defined(_18F66J60)
    ADCON0bits.ADCAL = 1;
    ADCON0bits.GO = 1;
    while (ADCON0bits.GO);
    ADCON0bits.ADCAL = 0;
#elif defined(__18F87J11) || defined(__18F86J16) || defined(__18F86J11) || defined(__18F67J11) || defined(__18F66J16) || defined(__18F66J11) || \
		   defined(_18F87J11) ||  defined(_18F86J16) ||  defined(_18F86J11) ||  defined(_18F67J11) ||  defined(_18F66J16) ||  defined(_18F66J11) || \
		  defined(__18F87J50) || defined(__18F86J55) || defined(__18F86J50) || defined(__18F67J50) || defined(__18F66J55) || defined(__18F66J50) || \
		   defined(_18F87J50) ||  defined(_18F86J55) ||  defined(_18F86J50) ||  defined(_18F67J50) ||  defined(_18F66J55) ||  defined(_18F66J50)
    ADCON1bits.ADCAL = 1;
    ADCON0bits.GO = 1;
    while (ADCON0bits.GO);
    ADCON1bits.ADCAL = 0;
#endif

#else	// 16-bit C30 and and 32-bit C32
#if defined(__PIC32MX__)
    {
        // Enable multi-vectored interrupts
        INTEnableSystemMultiVectoredInt();

        // Enable optimal performance
        SYSTEMConfigPerformance(GetSystemClock());
        mOSCSetPBDIV(OSC_PB_DIV_1); // Use 1:1 CPU Core:Peripheral clocks

        // Disable JTAG port so we get our I/O pins back, but first
        // wait 50ms so if you want to reprogram the part with
        // JTAG, you'll still have a tiny window before JTAG goes away.
        // The PIC32 Starter Kit debuggers use JTAG and therefore must not
        // disable JTAG.
        DelayMs(50);
#if !defined(__MPLAB_DEBUGGER_PIC32MXSK) && !defined(__MPLAB_DEBUGGER_FS2)
        DDPCONbits.JTAGEN = 0;
#endif
        LED_PUT(0x00); // Turn the LEDs off

        CNPUESET = 0x00098000; // Turn on weak pull ups on CN15, CN16, CN19 (RD5, RD7, RD13), which is connected to buttons on PIC32 Starter Kit boards
    }
#endif

#if defined(__dsPIC33F__) || defined(__PIC24H__)
    // Crank up the core frequency
    PLLFBD = 38; // Multiply by 40 for 160MHz VCO output (8MHz XT oscillator)
    CLKDIV = 0x0000; // FRC: divide by 2, PLLPOST: divide by 2, PLLPRE: divide by 2

    // Port I/O
    AD1PCFGHbits.PCFG23 = 1; // Make RA7 (BUTTON1) a digital input
    AD1PCFGHbits.PCFG20 = 1; // Make RA12 (INT1) a digital input for MRF24WB0M PICtail Plus interrupt

    // ADC
    AD1CHS0 = 0; // Input to AN0 (potentiometer)
    AD1PCFGLbits.PCFG5 = 0; // Disable digital input on AN5 (potentiometer)
    AD1PCFGLbits.PCFG4 = 0; // Disable digital input on AN4 (TC1047A temp sensor)
#else	//defined(__PIC24F__) || defined(__PIC32MX__)
#if defined(__PIC24F__)
    CLKDIVbits.RCDIV = 0; // Set 1:1 8MHz FRC postscalar
#endif

    // ADC
#if defined(__PIC24FJ256DA210__) || defined(__PIC24FJ256GB210__)
    // Disable analog on all pins
    ANSA = 0x0000;
    ANSB = 0x0000;
    ANSC = 0x0000;
    ANSD = 0x0000;
    ANSE = 0x0000;
    ANSF = 0x0000;
    ANSG = 0x0000;
#else
    AD1CHS = 0; // Input to AN0 (potentiometer)
    AD1PCFGbits.PCFG4 = 0; // Disable digital input on AN4 (TC1047A temp sensor)
#if defined(__32MX460F512L__) || defined(__32MX795F512L__)	// PIC32MX460F512L and PIC32MX795F512L PIMs has different pinout to accomodate USB module
    AD1PCFGbits.PCFG2 = 0; // Disable digital input on AN2 (potentiometer)
#else
    AD1PCFGbits.PCFG5 = 0; // Disable digital input on AN5 (potentiometer)
#endif
#endif
#endif

    // ADC
    AD1CON1 = 0x84E4; // Turn on, auto sample start, auto-convert, 12 bit mode (on parts with a 12bit A/D)
    AD1CON2 = 0x0404; // AVdd, AVss, int every 2 conversions, MUXA only, scan
    AD1CON3 = 0x1003; // 16 Tad auto-sample, Tad = 3*Tcy
#if defined(__32MX460F512L__) || defined(__32MX795F512L__)	// PIC32MX460F512L and PIC32MX795F512L PIMs has different pinout to accomodate USB module
    AD1CSSL = 1 << 2; // Scan pot
#else
    AD1CSSL = 1 << 5; // Scan pot
#endif

    // UART
#if defined(STACK_USE_UART)
    UARTTX_TRIS = 0;
    UARTRX_TRIS = 1;
    UMODE = 0x8000; // Set UARTEN.  Note: this must be done before setting UTXEN

#if defined(__C30__)
    USTA = 0x0400; // UTXEN set
#define CLOSEST_UBRG_VALUE ((GetPeripheralClock()+8ul*BAUD_RATE)/16/BAUD_RATE-1)
#define BAUD_ACTUAL (GetPeripheralClock()/16/(CLOSEST_UBRG_VALUE+1))
#else	//defined(__C32__)
    USTA = 0x00001400; // RXEN set, TXEN set
#define CLOSEST_UBRG_VALUE ((GetPeripheralClock()+8ul*BAUD_RATE)/16/BAUD_RATE-1)
#define BAUD_ACTUAL (GetPeripheralClock()/16/(CLOSEST_UBRG_VALUE+1))
#endif

#define BAUD_ERROR ((BAUD_ACTUAL > BAUD_RATE) ? BAUD_ACTUAL-BAUD_RATE : BAUD_RATE-BAUD_ACTUAL)
#define BAUD_ERROR_PRECENT	((BAUD_ERROR*100+BAUD_RATE/2)/BAUD_RATE)
#if (BAUD_ERROR_PRECENT > 3)
#warning UART frequency error is worse than 3%
#elif (BAUD_ERROR_PRECENT > 2)
#warning UART frequency error is worse than 2%
#endif

    UBRG = CLOSEST_UBRG_VALUE;
#endif

#endif

    // Deassert all chip select lines so there isn't any problem with
    // initialization order.  Ex: When ENC28J60 is on SPI2 with Explorer 16,
    // MAX3232 ROUT2 pin will drive RF12/U2CTS ENC28J60 CS line asserted,
    // preventing proper 25LC256 EEPROM operation.
#if defined(ENC_CS_TRIS)
    ENC_CS_IO = 1;
    ENC_CS_TRIS = 0;
#endif
#if defined(ENC100_CS_TRIS)
    ENC100_CS_IO = (ENC100_INTERFACE_MODE == 0);
    ENC100_CS_TRIS = 0;
#endif
#if defined(EEPROM_CS_TRIS)
    EEPROM_CS_IO = 1;
    EEPROM_CS_TRIS = 0;
#endif
#if defined(SPIRAM_CS_TRIS)
    SPIRAM_CS_IO = 1;
    SPIRAM_CS_TRIS = 0;
#endif
#if defined(SPIFLASH_CS_TRIS)
    SPIFLASH_CS_IO = 1;
    SPIFLASH_CS_TRIS = 0;
#endif
#if defined(WF_CS_TRIS)
    WF_CS_IO = 1;
    WF_CS_TRIS = 0;
#endif

#if defined(PIC24FJ64GA004_PIM)
    __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS

    // Remove some LED outputs to regain other functions
    LED1_TRIS = 1; // Multiplexed with BUTTON0
    LED5_TRIS = 1; // Multiplexed with EEPROM CS
    LED7_TRIS = 1; // Multiplexed with BUTTON1

    // Inputs
    RPINR19bits.U2RXR = 19; //U2RX = RP19
    RPINR22bits.SDI2R = 20; //SDI2 = RP20
    RPINR20bits.SDI1R = 17; //SDI1 = RP17

    // Outputs
    RPOR12bits.RP25R = U2TX_IO; //RP25 = U2TX
    RPOR12bits.RP24R = SCK2OUT_IO; //RP24 = SCK2
    RPOR10bits.RP21R = SDO2_IO; //RP21 = SDO2
    RPOR7bits.RP15R = SCK1OUT_IO; //RP15 = SCK1
    RPOR8bits.RP16R = SDO1_IO; //RP16 = SDO1

    AD1PCFG = 0xFFFF; //All digital inputs - POT and Temp are on same pin as SDO1/SDI1, which is needed for ENC28J60 commnications

    __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS
#endif

#if defined(__PIC24FJ256DA210__)
    __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS

    // Inputs
    RPINR19bits.U2RXR = 11; // U2RX = RP11
    RPINR20bits.SDI1R = 0; // SDI1 = RP0
    RPINR0bits.INT1R = 34; // Assign RE9/RPI34 to INT1 (input) for MRF24WB0M Wi-Fi PICtail Plus interrupt

    // Outputs
    RPOR8bits.RP16R = 5; // RP16 = U2TX
    RPOR1bits.RP2R = 8; // RP2 = SCK1
    RPOR0bits.RP1R = 7; // RP1 = SDO1

    __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS
#endif

#if defined(__PIC24FJ256GB110__) || defined(__PIC24FJ256GB210__)
    __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS

    // Configure SPI1 PPS pins (ENC28J60/ENCX24J600/MRF24WB0M or other PICtail Plus cards)
    RPOR0bits.RP0R = 8; // Assign RP0 to SCK1 (output)
    RPOR7bits.RP15R = 7; // Assign RP15 to SDO1 (output)
    RPINR20bits.SDI1R = 23; // Assign RP23 to SDI1 (input)

    // Configure SPI2 PPS pins (25LC256 EEPROM on Explorer 16)
    RPOR10bits.RP21R = 11; // Assign RG6/RP21 to SCK2 (output)
    RPOR9bits.RP19R = 10; // Assign RG8/RP19 to SDO2 (output)
    RPINR22bits.SDI2R = 26; // Assign RG7/RP26 to SDI2 (input)

    // Configure UART2 PPS pins (MAX3232 on Explorer 16)
#if !defined(ENC100_INTERFACE_MODE) || (ENC100_INTERFACE_MODE == 0) || defined(ENC100_PSP_USE_INDIRECT_RAM_ADDRESSING)
    RPINR19bits.U2RXR = 10; // Assign RF4/RP10 to U2RX (input)
    RPOR8bits.RP17R = 5; // Assign RF5/RP17 to U2TX (output)
#endif

    // Configure INT1 PPS pin (MRF24WB0M Wi-Fi PICtail Plus interrupt signal when in SPI slot 1)
    RPINR0bits.INT1R = 33; // Assign RE8/RPI33 to INT1 (input)

    // Configure INT3 PPS pin (MRF24WB0M Wi-Fi PICtail Plus interrupt signal when in SPI slot 2)
    RPINR1bits.INT3R = 40; // Assign RC3/RPI40 to INT3 (input)

    __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS
#endif

#if defined(__PIC24FJ256GA110__)
    __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS

    // Configure SPI2 PPS pins (25LC256 EEPROM on Explorer 16 and ENC28J60/ENCX24J600/MRF24WB0M or other PICtail Plus cards)
    // Note that the ENC28J60/ENCX24J600/MRF24WB0M PICtails SPI PICtails must be inserted into the middle SPI2 socket, not the topmost SPI1 slot as normal.  This is because PIC24FJ256GA110 A3 silicon has an input-only RPI PPS pin in the ordinary SCK1 location.  Silicon rev A5 has this fixed, but for simplicity all demos will assume we are using SPI2.
    RPOR10bits.RP21R = 11; // Assign RG6/RP21 to SCK2 (output)
    RPOR9bits.RP19R = 10; // Assign RG8/RP19 to SDO2 (output)
    RPINR22bits.SDI2R = 26; // Assign RG7/RP26 to SDI2 (input)

    // Configure UART2 PPS pins (MAX3232 on Explorer 16)
    RPINR19bits.U2RXR = 10; // Assign RF4/RP10 to U2RX (input)
    RPOR8bits.RP17R = 5; // Assign RF5/RP17 to U2TX (output)

    // Configure INT3 PPS pin (MRF24WB0M PICtail Plus interrupt signal)
    RPINR1bits.INT3R = 36; // Assign RA14/RPI36 to INT3 (input)

    __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS
#endif


#if defined(DSPICDEM11)
    // Deselect the LCD controller (PIC18F252 onboard) to ensure there is no SPI2 contention
    LCDCTRL_CS_TRIS = 0;
    LCDCTRL_CS_IO = 1;

    // Hold the codec in reset to ensure there is no SPI2 contention
    CODEC_RST_TRIS = 0;
    CODEC_RST_IO = 0;
#endif

#if defined(SPIRAM_CS_TRIS)
    SPIRAMInit();
#endif
#if defined(EEPROM_CS_TRIS)
    XEEInit();
#endif
#if defined(SPIFLASH_CS_TRIS)
    SPIFlashInit();
#endif
}

/*********************************************************************
 * Function:        void InitAppConfig(void)
 *
 * PreCondition:    MPFSInit() is already called.
 *
 * Input:           None
 *
 * Output:          Write/Read non-volatile config variables.
 *
 * Side Effects:    None
 *
 * Overview:        None
 *
 * Note:            None
 ********************************************************************/
// MAC Address Serialization using a MPLAB PM3 Programmer and 
// Serialized Quick Turn Programming (SQTP). 
// The advantage of using SQTP for programming the MAC Address is it
// allows you to auto-increment the MAC address without recompiling 
// the code for each unit.  To use SQTP, the MAC address must be fixed
// at a specific location in program memory.  Uncomment these two pragmas
// that locate the MAC address at 0x1FFF0.  Syntax below is for MPLAB C 
// Compiler for PIC18 MCUs. Syntax will vary for other compilers.
//#pragma romdata MACROM=0x1FFF0
static ROM BYTE SerializedMACAddress[6] = {MY_DEFAULT_MAC_BYTE1, MY_DEFAULT_MAC_BYTE2, MY_DEFAULT_MAC_BYTE3, MY_DEFAULT_MAC_BYTE4, MY_DEFAULT_MAC_BYTE5, MY_DEFAULT_MAC_BYTE6};
//#pragma romdata

static void InitAppConfig(void) {
    BYTE co[2];
    AppConfig.Flags.bIsDHCPEnabled = FALSE;
    AppConfig.Flags.bIsPort = FALSE;
    // Port80_oppure_8085 = 80;
    AppConfig.Flags.bInConfigMode = TRUE;

    AppConfig.PasswordWeb[0] = 'f';
    AppConfig.PasswordWeb[1] = 't';
    AppConfig.PasswordWeb[2] = 'p';
    AppConfig.PasswordWeb[3] = 0;

    memcpypgm2ram((void*) &AppConfig.MyMACAddr, (ROM void*) SerializedMACAddress, sizeof (AppConfig.MyMACAddr));
    //	{
    //		_prog_addressT MACAddressAddress;
    //		MACAddressAddress.next = 0x157F8;
    //		_memcpy_p2d24((char*)&AppConfig.MyMACAddr, MACAddressAddress, sizeof(AppConfig.MyMACAddr));
    //	}
    AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_BYTE2 << 8ul | MY_DEFAULT_IP_ADDR_BYTE3 << 16ul | MY_DEFAULT_IP_ADDR_BYTE4 << 24ul;
    AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val;
    AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2 << 8ul | MY_DEFAULT_MASK_BYTE3 << 16ul | MY_DEFAULT_MASK_BYTE4 << 24ul;
    AppConfig.DefaultMask.Val = AppConfig.MyMask.Val;
    AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2 << 8ul | MY_DEFAULT_GATE_BYTE3 << 16ul | MY_DEFAULT_GATE_BYTE4 << 24ul;
    AppConfig.PrimaryDNSServer.Val = MY_DEFAULT_PRIMARY_DNS_BYTE1 | MY_DEFAULT_PRIMARY_DNS_BYTE2 << 8ul | MY_DEFAULT_PRIMARY_DNS_BYTE3 << 16ul | MY_DEFAULT_PRIMARY_DNS_BYTE4 << 24ul;
    AppConfig.SecondaryDNSServer.Val = MY_DEFAULT_SECONDARY_DNS_BYTE1 | MY_DEFAULT_SECONDARY_DNS_BYTE2 << 8ul | MY_DEFAULT_SECONDARY_DNS_BYTE3 << 16ul | MY_DEFAULT_SECONDARY_DNS_BYTE4 << 24ul;

    // Load the default NetBIOS Host Name
    memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*) MY_DEFAULT_HOST_NAME, 16);
    FormatNetBIOSName(AppConfig.NetBIOSName);


    // SNMP Community String configuration
#if defined(STACK_USE_SNMP_SERVER)
    {
        BYTE i;
        static ROM char * ROM cReadCommunities[] = SNMP_READ_COMMUNITIES;
        static ROM char * ROM cWriteCommunities[] = SNMP_WRITE_COMMUNITIES;
        ROM char * strCommunity;

        for (i = 0; i < SNMP_MAX_COMMUNITY_SUPPORT; i++) {
            // Get a pointer to the next community string
            strCommunity = cReadCommunities[i];
            if (i >= sizeof (cReadCommunities) / sizeof (cReadCommunities[0]))
                strCommunity = "";

            // Ensure we don't buffer overflow.  If your code gets stuck here,
            // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h
            // is either too small or one of your community string lengths
            // (SNMP_READ_COMMUNITIES) are too large.  Fix either.
            if (strlenpgm(strCommunity) >= sizeof (AppConfig.readCommunity[0]))
                while (1);

            // Copy string into AppConfig
            strcpypgm2ram((char*) AppConfig.readCommunity[i], strCommunity);

            // Get a pointer to the next community string
            strCommunity = cWriteCommunities[i];
            if (i >= sizeof (cWriteCommunities) / sizeof (cWriteCommunities[0]))
                strCommunity = "";

            // Ensure we don't buffer overflow.  If your code gets stuck here,
            // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h
            // is either too small or one of your community string lengths
            // (SNMP_WRITE_COMMUNITIES) are too large.  Fix either.
            if (strlenpgm(strCommunity) >= sizeof (AppConfig.writeCommunity[0]))
                while (1);

            // Copy string into AppConfig
            strcpypgm2ram((char*) AppConfig.writeCommunity[i], strCommunity);
        }
    }
#endif

    // Load the default NetBIOS Host Name
    memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*) MY_DEFAULT_HOST_NAME, 16);
    FormatNetBIOSName(AppConfig.NetBIOSName);


#if defined(EEPROM_CS_TRIS)
    {
        BYTE c;

        // When a record is saved, first byte is written as 0x60 to indicate
        // that a valid record was saved.  Note that older stack versions
        // used 0x57.  This change has been made to so old EEPROM contents
        // will get overwritten.  The AppConfig() structure has been changed,
        // resulting in parameter misalignment if still using old EEPROM
        // contents.
        XEEReadArray(0x0000, &c, 1);
        if (c == 0x61u)
            XEEReadArray(0x0001, (BYTE*) & AppConfig, sizeof (AppConfig));
        else
            SaveAppConfig();
    }
#elif defined(SPIFLASH_CS_TRIS)
    {
        BYTE c;

        SPIFlashReadArray(0x0000, &c, 1);
        if (c == 0x61u)
            SPIFlashReadArray(0x0001, (BYTE*) & AppConfig, sizeof (AppConfig));
        else
            SaveAppConfig();
    }
#endif

    if (AppConfig.Flags.bIsPort == FALSE) {
        Port80_oppure_8085 = 80;
    } else {
        Port80_oppure_8085 = 8085;
    }

    XEEReadArray(extEEaddr_NumPorta, co, 2);
    Port80_oppure_xxx = co[0]*256 + co[1];
    if ((Port80_oppure_xxx < 80) || (Port80_oppure_xxx > 65534)) {
        Port80_oppure_xxx = 80;
    }
    // alb: to be used in case of panic
    // if (Port80_oppure_xxx != 80){Port80_oppure_xxx=80;}
}

#if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS)

void SaveAppConfig(void) {
    // Ensure adequate space has been reserved in non-volatile storage to
    // store the entire AppConfig structure.  If you get stuck in this while(1)
    // trap, it means you have a design time misconfiguration in TCPIPConfig.h.
    // You must increase MPFS_RESERVE_BLOCK to allocate more space.
#if defined(STACK_USE_MPFS) || defined(STACK_USE_MPFS2)
    if (sizeof (AppConfig) > MPFS_RESERVE_BLOCK)
        while (1);
#endif

#if defined(EEPROM_CS_TRIS)
    XEEBeginWrite(0x0000);
    XEEWrite(0x61);
    XEEWriteArray((BYTE*) & AppConfig, sizeof (AppConfig));
#else
    SPIFlashBeginWrite(0x0000);
    SPIFlashWrite(0x61);
    SPIFlashWriteArray((BYTE*) & AppConfig, sizeof (AppConfig));
#endif
}
#endif

BYTE clear(BYTE *c) {
    int j;
    int b = sizeof (c);
    for (j = 0; j < 15; j++) {
        *(c + j) = 0;
    }
}